//
//  TableViewDelegate.m
//  JFTQRLOGIN
//
//  Created by mac on 2017/12/4.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "TableViewDelegate.h"
#import "MessageModel.h"
static const int inset = 10;
static NSString* const kChineseFont = @"STHeitiSC-Light";
static const CGFloat kContentFontSize = 15.0f;
NS_ASSUME_NONNULL_BEGIN
@interface TableViewDelegate ()
@property (nonatomic,strong)UITableView *table_view;
@property (nonatomic,assign)NSInteger it_count;
@property (nonatomic,strong)NSString *cell_identifier;
@end
NS_ASSUME_NONNULL_END
@implementation TableViewDelegate
- (instancetype)initWithTableView:(UITableView *)tableView listCell:(NSString *)cellIdentifier itemCount:(NSInteger)itCount{
    if (self = [super init]) {
        self.it_count = itCount;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.table_view = tableView;
        self.cell_identifier = cellIdentifier;
    }
    return self;
    
}
- (void)reloadMyData{
    [self.table_view reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArrayItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Class cls =NSClassFromString(self.className);
    UITableViewCell *cell =(UITableViewCell*)cls;
     cell = [tableView dequeueReusableCellWithIdentifier:self.cell_identifier];
    if (!cell) {
        if ([self.className isEqualToString:@"UITableViewCell"]) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.cell_identifier];
        }else{
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:self.className owner:nil options:nil];
        cell = [nibs lastObject];
        }
    }
    self.block(indexPath,cell);
    return  cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MessageModel *model =[self.itemArrayItems objectAtIndex:indexPath.row];
    return [self calcHeightForContent:model.content WithWidth:[UIScreen mainScreen].bounds.size.width-40]+34;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    self.selectBlock(indexPath);
}

//- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
//    self.scrollviewBlock(scrollView.contentOffset.y);
//
//}
//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//    self.scrollviewBlock(scrollView.contentOffset.y);
//}
//- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
//    self.scrollviewBlock(scrollView.contentOffset.y);
//
//}
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//    self.scrollviewBlock(scrollView.contentOffset.y);
//
//}
//- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
//    self.scrollviewBlock(scrollView.contentOffset.y);
//
//}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
        self.scrollviewBlock(scrollView.contentOffset.y);

}
- (CGFloat)calcHeightForContent:(NSString *)contentString
                      WithWidth:(CGFloat)width {
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    paraStyle.minimumLineHeight = inset * 2;
    return CGRectGetHeight([contentString boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                                                       options:NSStringDrawingUsesLineFragmentOrigin
                                                    attributes:@{                                                                 NSFontAttributeName: [UIFont fontWithName:kChineseFont
                                                                                            size:kContentFontSize],
                                                                                                                    NSParagraphStyleAttributeName: paraStyle
                                                                 }
                                                       context:nil]);
}
@end
